<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            height: 2000px;
        }
        .btn{
            width: 100px;
            height: 100px;
            background-color: pink;
            text-align: center;
            line-height: 100px;
            position: fixed;
            bottom: 200px;
            right: 100px;
            display: none;
        }
    </style>
</head>
<body>
    <button class="btn">
        回到顶部    
    </button>

    <script>
        let btn=document.querySelector(".btn")
        console.log(btn);
        window.addEventListener("scroll",function(){
            if(scrollY>=500){
                btn.style.display="block"
            }else{
                btn.style.display="none"
            }
        })
        let time=null
        btn.addEventListener("click",function(){
            // scrollTo(0,0)
            time=setInterval(function(){
                window.scrollTo(0,scrollY-10)
                if(scrollY<=0){
                    clearInterval(time)
                }
            },50)
            
        })
    </script>
</body>
</html>